package com.eliansun.crawler.pageparse.zhejiang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.eliansun.crawler.pageparse.AbstractPageProcess;
import com.eliansun.util.SpiderUtil;
import net.dongliu.requests.Requests;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liujin
 * @description https://s.taobao.com/search?q=@key@&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20190606&ie=utf8
 * @create 2019/6/5
 */
public class TB extends AbstractPageProcess {

    int count = 0;
    int pageNum = 0;
    int pageCount = 1;

    private static final Logger LOGGER = LoggerFactory.getLogger(TB.class);

    private Site site = Site.me().setCycleRetryTimes(5).setRetryTimes(5).setSleepTime(2000).setTimeOut(3 * 60 * 1000)
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
            .addHeader("Accept-Language", "zzh-CN,zh;q=0.8")
            .addHeader("Accept-Encoding", "zh-CN,zh;q=0.8");

    @Override
    public void process(Page page) {
        if (page.getUrl().toString().indexOf("detail") != -1) {
            List<String> titleList = page.getHtml().xpath("//ul[@id='J_AttrUL']//li//text()").all();
            List<String> infoList = page.getHtml().xpath("//ul[@id='J_AttrUL']//li//@title").all();
            if (CollectionUtils.isEmpty(titleList)) {
                titleList = page.getHtml().xpath("//ul[@class='attributes-list']//li//text()").all();
                infoList = page.getHtml().xpath("//ul[@class='attributes-list']//li//@title").all();
            }
            Map<String, Object> resMap = new HashMap<>();
            String goodsName = "";
            for (int i = 0; i < titleList.size(); i++) {
                if (titleList.get(i).indexOf("产品名称") != -1) {
                    goodsName = infoList.get(i).trim();
                }
                if (titleList.get(i).indexOf("品牌") != -1) {
                    resMap.put("pp", infoList.get(i).trim());
                }
                if (titleList.get(i).contains("规格")) {
                    resMap.put("gg", infoList.get(i).trim());
                }
                if (titleList.get(i).contains("剂型")) {
                    resMap.put("xh", infoList.get(i).trim());
                }
                if (titleList.get(i).contains("批准文号")) {
                    resMap.put("zch", infoList.get(i).trim());
                }
                if (titleList.get(i).contains("生产企业")) {
                    resMap.put("sccj", infoList.get(i).trim());
                }
                if (titleList.get(i).contains("厂名")) {
                    resMap.put("sccj", infoList.get(i));
                }
            }
            if (StringUtils.isEmpty(goodsName)) {
                goodsName = page.getHtml().xpath("//div[@class='tb-detail-hd']//h1//text()").toString();
            }
            if (StringUtils.isEmpty(goodsName)) {
                goodsName = page.getHtml().xpath("//div[@id='J_Title']//h3//@data-title").toString();
            }

            String price = page.getUrl().toString().substring(page.getUrl().toString().indexOf("price=") + 6);
            resMap.put("price", price);
            resMap.put("goodsName", goodsName.trim());
//            String store = page.getHtml().xpath("//span[@class='mlogo']//a//@title").toString();



            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("spec", resMap.get("gg"));
            resultMap.put("medicalDeviceNo", resMap.get("zch"));
            resultMap.put("productCompany", resMap.get("sccj"));
            resultMap.put("price", resMap.get("price"));
            resultMap.put("brand", resMap.get("pp"));
            resultMap.put("productName", resMap.get("goodsName"));
            resultMap.put("executiveStandard", "");
            resultMap.put("nationality", "");
            resultMap.put("store", super.cTerrace);
            resultMap.put("url", page.getUrl());
            resultMap.put("version", resMap.get("xh"));
            page.putField("result", resultMap);
            count++;
            System.out.println(count);
        } else {
            LOGGER.info("这是第" + pageCount + "页数据");
            pageNum = pageNum + 44;
            if(pageCount > 1){
                try {
                    Thread.sleep(300000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            String fristUrl = page.getUrl().toString();
            //判断当前产品是否有效
            String notFoundHtml = Requests.get(fristUrl).headers(SpiderUtil.getTBHeader()).send().readToText();
            String notFoundscriptStr = Jsoup.parse(notFoundHtml).getElementsByTag("script").eq(7).html();
            String notFoundPageJson = notFoundscriptStr.substring(16, notFoundscriptStr.indexOf("};") + 1);
            String notFoundStr = JSON.parseObject(notFoundPageJson).getJSONObject("mods").getJSONObject("pager").get("status").toString();
            if (notFoundStr.equals("hide")) {
                return;
            }

            List<String> detailList = new ArrayList<>();
            //获取当前页面所有产品id，组成详情页地址
            JSONArray ids = JSON.parseObject(notFoundPageJson).getJSONObject("mainInfo")
                    .getJSONObject("traceInfo")
                    .getJSONObject("traceData").getJSONArray("auctionNids");
            JSONArray priceList = JSON.parseObject(notFoundPageJson).getJSONObject("mainInfo")
                    .getJSONObject("traceInfo")
                    .getJSONObject("traceData").getJSONArray("allPrices");
            for (int i = 0; i < ids.size(); i++) {
                detailList.add("https://item.taobao.com/item.htm?spm=a230r.1.14.1.17095d97An0id3&id=" + ids.getString(i) + "&ns=1&abbucket=17#detail&price=" + priceList.getString(i));
            }
            page.addTargetRequests(detailList);

            //下一页
            String nextPageUrl = page.getUrl().toString().substring(0, page.getUrl().toString().indexOf("&s=")) + "&s=" + pageNum;
            page.addTargetRequest(nextPageUrl);
            pageCount++;

        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new TB())
//                .addUrl("https://item.taobao.com/item.htm?spm=a230r.1.14.1.17095d97An0id3&id=593246550257&ns=1&abbucket=17#detail")
                .addUrl("https://s.taobao.com/search?q=@key@&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20190606&ie=utf8&s=0")
                .thread(1)
                .run();
    }
}